var webPage = require('webpage');


var urls = [ 
'&DOCUMENT_LABEL=User&DOCUMENT_NAME=User&document=326&SBI_EXECUTION_ID=386c4fb4a55e11e6affb8d6f69a95323',
'&DOCUMENT_LABEL=changeOwner&DOCUMENT_NAME=changeOwner&document=319&SBI_EXECUTION_ID=6fed24a8a55e11e6affb8d6f69a95323',
'&DOCUMENT_LABEL=cpdfDoc&DOCUMENT_NAME=changeOwner&document=317&SBI_EXECUTION_ID=8145736aa55e11e6affb8d6f69a95323',
'&DOCUMENT_LABEL=cpdfReview&DOCUMENT_NAME=cpdfReview&document=318&SBI_EXECUTION_ID=9ba74bcca55e11e6affb8d6f69a95323',
'&DOCUMENT_LABEL=cpdfUpload&DOCUMENT_NAME=cpdfUpload&document=320&SBI_EXECUTION_ID=e4f58fc7a56f11e6affb8d6f69a95323',
'&DOCUMENT_LABEL=cpdfVer&DOCUMENT_NAME=cpdfVer&document=321&SBI_EXECUTION_ID=05507d79a57011e6affb8d6f69a95323',
'&DOCUMENT_LABEL=docHomeMsg&DOCUMENT_NAME=docHomeMsg&document=322&SBI_EXECUTION_ID=1597aa5ba57011e6affb8d6f69a95323',
'&DOCUMENT_LABEL=docSearchMsg&DOCUMENT_NAME=docSearchMsg&document=323&SBI_EXECUTION_ID=227a768da57011e6affb8d6f69a95323',
'&DOCUMENT_LABEL=drmEnforceTrack&DOCUMENT_NAME=drmEnforceTrack&document=324&SBI_EXECUTION_ID=38b317a1a57011e6affb8d6f69a95323',
'&DOCUMENT_LABEL=drmProtected&DOCUMENT_NAME=drmProtected&document=325&SBI_EXECUTION_ID=44a47ef3a57011e6affb8d6f69a95323',
'&DOCUMENT_LABEL=fromUser&DOCUMENT_NAME=fromUser&document=328&SBI_EXECUTION_ID=5a27d9c5a57011e6affb8d6f69a95323',
'&DOCUMENT_LABEL=signupUser&DOCUMENT_NAME=signupUser&document=327&SBI_EXECUTION_ID=69f6f079a57011e6affb8d6f69a95323'
];


var names=["User" ,"changeOwner","cpdfDoc","cpdfReview","cpdfUpload","cpdfVer","docHomeMsg","docSearchMsg","drmEnforceTrack" ,"drmProtected","fromUser","signupUser"];

renderSpago(urls ,names );




function getFormatTime(timestamp) {
  newDate = new Date(timestamp)
  month = newDate.getMonth() + 1;
  date = newDate.getDate()
  if (month < 10) {
    month = '0' + month
  }
  if (date < 10) {
    date = '0' + date
  }
  formatTime = [
    newDate.getFullYear(),
    month,
    date
  ].join('-')  // alert(formatTime)
  return formatTime
}


function renderSpago (urls , names) {

  var myDate = new Date()
  var cTimestamp = (new Date())
  today = getFormatTime(cTimestamp - 1 * 24 * 3600 * 1000);
  aweekago = getFormatTime(cTimestamp - 7 * 24 * 3600 * 1000);

    var i = urls.length;
  console.log(i);
  if (!i) {
    return phantom.exit();
  }
    var url = urls.pop();
    var name = names.pop();

console.log("---> From " + aweekago + "  to  " + today);
var baseUrl = 'http://spago.sumilux.com/SpagoBIChartEngine/servlet/AdapterHTTP?user_id=biadmin&Doc&userID=biadmin&password=spago123lux&pStartDay_description=&DOCUMENT_COMMUNITIES=[]&DOCUMENT_IS_VISIBLE=true&SBI_EXECUTION_ROLE=/spagobi/admin&pStartDay=aweekago&SBICONTEXT=/SpagoBI&ACTION_NAME=CHART_ENGINE_START_ACTION&DOCUMENT_FUNCTIONALITIES=[45]&SBI_COUNTRY=US&DOCUMENT_AUTHOR=biadmin&DOCUMENT_DESCRIPTION=&IS_TECHNICAL_USER=true&SBI_SPAGO_CONTROLLER=/servlet/AdapterHTTP&language=en&country=US&pEndDay_description=&SBI_LANGUAGE=en&pEndDay=today&NEW_SESSION=TRUE&DOCUMENT_IS_PUBLIC=true&DOCUMENT_VERSION=1512&SBI_HOST=http://spago.sumilux.com:80&MODALITY=VIEW&isFromCross=false&SBI_ENVIRONMENT=DOCBROWSER';
var cpdfurl = baseUrl + url;
cpdfurl = cpdfurl.replace('aweekago', aweekago).replace('today', today);
console.log("---> " + name + " Url -----------------" );
console.log(cpdfurl);

    var page = require('webpage').create();
    page.viewportSize = {width: 1600, height: 1024};
  console.log('start', cpdfurl);

page.open(cpdfurl, function() {

 window.setTimeout(function () {
  page.open(cpdfurl, function() {
     window.setTimeout(function () {

    console.log("---> capture the screen data -----------------");
    page.render(name+'.png');
    page.render(name+'.pdf');
    console.log("---> Finish capture:" + name+'.png And' + name+'.pdf')
    console.log("---> urls.length:" + urls.length)
    renderSpago(urls , names);

}, 60000 );
});
}, 60000 );
});
}



